package com.example.nonacademic_education.util.timer;

import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * @author xinmiao
 * @description
 * @date 2023/4/1 21:32
 */
@Service
public class QuartzUtil {
    @Resource
    private Scheduler scheduler;
    public Boolean add(String id, String corn) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(DelPdfFile.class)
                /*给当前JobDetail添加参数，K V形式*/
                .usingJobData("id", id)
                /*添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(id)
                .build();//执行
        CronTrigger trigger= TriggerBuilder.newTrigger()
                .withIdentity(id)
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(corn))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        if (!scheduler.isShutdown()) {
            /*启动*/
            scheduler.start();
        }
        return true;
    }
    public Boolean update(String id,String corn) {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(id);
            //获取当前正在运行的触发器
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if(Objects.isNull(trigger)){
                return false;
            }
            //根据新的执行器重新关联触发器
            trigger = trigger
                    .getTriggerBuilder()
                    .withIdentity(id)
                    .withSchedule(CronScheduleBuilder.cronSchedule(corn))
                    .build();
            //重置对应的job
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public Boolean del(String id) throws IOException, SchedulerException {
        scheduler.pauseTrigger(TriggerKey.triggerKey(id));//暂停触发器
        scheduler.unscheduleJob(TriggerKey.triggerKey(id));//移除触发器
        scheduler.deleteJob(JobKey.jobKey(id));//删除Job
        return true;
    }
    public Boolean addKapchaCodeTimer(String id, String corn) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(CodesClearJob.class)
                /*给当前JobDetail添加参数，K V形式*/
                .usingJobData("id", id)
                /*添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(id)
                .build();//执行
        CronTrigger trigger= TriggerBuilder.newTrigger()
                .withIdentity(id)
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(corn))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        if (!scheduler.isShutdown()) {
            /*启动*/
            scheduler.start();
        }
        return true;
    }
    public Boolean addBackupsTimer(String id, String corn) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(MysqlBackups.class)
                /*给当前JobDetail添加参数，K V形式*/
                .usingJobData("id", id)
                /*添加认证信息，有3种重写的方法，我这里是其中一种，可以查看源码看其余2种*/
                .withIdentity(id)
                .build();//执行
        CronTrigger trigger= TriggerBuilder.newTrigger()
                .withIdentity(id)
                .withSchedule(CronScheduleBuilder
                        .cronSchedule(corn))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        if (!scheduler.isShutdown()) {
            /*启动*/
            scheduler.start();
        }
        return true;
    }
}
